package com.ssm.web.action;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ssm.bean.ItemCheck;
import com.ssm.bean.Teacher;
import com.ssm.util.Constant;
import com.ssm.util.FieldUtil;
import com.ssm.util.MD5;
import com.ssm.util.MapUtil;
import com.ssm.util.MathExtend;
import com.ssm.util.MessageUtil;
import com.ssm.util.Page;
import com.ssm.util.SessionBean;
import com.ssm.util.common.BaseAction;
import com.ssm.web.service.ItemCheckService;
import com.ssm.web.service.SpecService;
import com.ssm.web.service.TeacherService;

@Controller
@RequestMapping("/sys")
public class TeacherAction extends BaseAction {
	@Autowired
	private SpecService				specService;
	@Autowired
	private ItemCheckService	itemCheckService;
	private String						actionname	= "教师";
	private String						actionclass	= "Teacher";
	@Autowired
	private TeacherService		service;

	@RequestMapping(value = "/updateTeacherInfo.do")
	public String updateTeacherInfo(Teacher bean) {
		Teacher user = getTeacher();
		//		user = sysUserService.get(user.getId());
		if (StringUtils.isNotBlank(bean.getUserPassword())) {
			MD5 md = new MD5();
			String password = md.getMD5ofStr(bean.getUserPassword());
			bean.setUserPassword(password);
		} else {
			bean.setUserPassword(user.getUserPassword());
		}
		bean.setId(user.getId());

		service.updateAll(bean);
		SessionBean sb = (SessionBean) session.getAttribute(Constant.SESSION_BEAN);
		sb.setUser(service.get(bean.getId()));

		MessageUtil.addMessage(request, "修改成功.");
		return SUCCESS;
	}

	@RequestMapping(value = "/modifyTeacherPassword.do")
	public String modifyTeacherPassword(String oldpwd, String userPassword) {
		try {
			MD5 md = new MD5();

			Teacher user = service.get(getTeacher().getId());
			String p = md.getMD5ofStr(oldpwd);
			if (!p.equals(user.getUserPassword())) {
				MessageUtil.addMessage(request, "旧密码错误.");
				return ERROR;
			}
			user.setUserPassword(md.getMD5ofStr(userPassword));
			service.updateAll(user);

			MessageUtil.addMessage(request, "修改成功.");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
		}
		MessageUtil.addMessage(request, "修改失败.");
		return ERROR;
	}

	@RequestMapping(value = "/add2Teacher.do")
	public String add2() {
		putRequestValue("list", specService.selectAll());
		request.setAttribute("actionname", actionname);
		request.setAttribute("actionclass", actionclass);
		return "addTeacher";
	}

	@RequestMapping(value = "/getTeacher.do")
	public String get(int uid) {
		try {
			putRequestValue("list", specService.selectAll());
			Teacher bean = service.get(uid);
			request.setAttribute("modifybean", bean);

			request.setAttribute("actionname", actionname);
			request.setAttribute("actionclass", actionclass);
			return "modifyTeacher";
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "获取信息失败.");
			return ERROR;
		}
	}

	@RequestMapping(value = "/deleteTeacher.do")
	public String delete(String ids) {
		try {
			service.deleteTeacher(ids);
			MessageUtil.addRelMessage(request, "删除信息成功.", "mainquery");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "删除信息失败.");
			return ERROR;
		}
	}

	@RequestMapping(value = "/addTeacher.do")
	public String add(Teacher bean) {
		try {
			service.addTeacher(bean);
			MessageUtil.addMessage(request, "添加成功.");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "添加失败.");
			return ERROR;
		}

	}

	@RequestMapping(value = "/updateTeacher.do")
	public String update(Teacher bean) {
		try {
			service.updateTeacher(bean);
			if (getSessionUser() instanceof Teacher) {
				if (getTeacher().getId().equals(bean.getId())) {
					SessionBean sb = (SessionBean) getSessionValue(Constant.SESSION_BEAN);
					sb.setUser(service.get(getTeacher().getId()));
				}
			}
			MessageUtil.addMessage(request, "更新成功.");
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			MessageUtil.addMessage(request, "更新失败.");
			return ERROR;
		}
	}

	@RequestMapping(value = "/queryTeacher.do")
	public String query() {
		try {
			// 字段名称集合
			LinkedList<String> parmnames = new LinkedList<String>();
			// 字段值集合
			LinkedList<Object> parmvalues = new LinkedList<Object>();
			Page p = FieldUtil.createPage(request, Teacher.class, parmnames, parmvalues);

			Page page = service.selectPage(p, Teacher.class);
			session.setAttribute(Constant.SESSION_PAGE, page);

			request.setAttribute("actionname", actionname);
			request.setAttribute("actionclass", actionclass);
			return "listTeacher";
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	@RequestMapping(value = "/querySelfSpecTecher.do")
	public String querySelfSpecTecher() {
		try {
			// 字段名称集合
			LinkedList<String> parmnames = new LinkedList<String>();
			// 字段值集合
			LinkedList<Object> parmvalues = new LinkedList<Object>();
			Page p = FieldUtil.createPage(request, Teacher.class, parmnames, parmvalues);

			parmnames.add("specId");
			parmvalues.add(getTeacher().getSpecId());

			Page page = service.selectPage(p, Teacher.class);
			session.setAttribute(Constant.SESSION_PAGE, page);

			request.setAttribute("actionname", actionname);
			request.setAttribute("actionclass", actionclass);
			return "querySelfSpecTecher";
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	@RequestMapping(value = "/queryTeacherScore.do")
	public String queryTeacherScore() {
		try {
			String s_uname = getString("s_uname");
			String s_userName = getString("s_userName");
			Map<String, Object> map = new HashMap<String, Object>();
			if (StringUtils.isNotBlank(s_uname)) {
				map.put("uname", "%" + s_uname + "%");
			}
			if (StringUtils.isNotBlank(s_userName)) {
				map.put("userName", "%" + s_userName + "%");
			}
			List<Teacher> list = service.selectAll(Teacher.class, map);
			for (Teacher teacher : list) {
				double fen = 0d;
				List<ItemCheck> selectAll = itemCheckService.selectAll(ItemCheck.class, MapUtil.createMap("teacherId", teacher.getId(), "typesel", "学生评教"));
				for (ItemCheck itemCheck : selectAll) {
					fen += itemCheck.getScore();
				}
				if (selectAll.size() > 0) {
					teacher.setXuesheng(MathExtend.round(fen / selectAll.size(), 1));
				}
				teacher.setRen(selectAll.size());

				teacher.setScore(teacher.getXuesheng() * 100 / 100);
				teacher.setScore(MathExtend.round(teacher.getScore(), 1));

			}

			Collections.sort(list, new Comparator<Teacher>() {

				@Override
				public int compare(Teacher o1, Teacher o2) {
					if (o2.getScore() > o1.getScore())
						return 1;
					if (o2.getScore() == o1.getScore())
						return 0;
					return -1;

				}
			});

			putRequestValue("list", list);

			request.setAttribute("actionname", actionname);
			request.setAttribute("actionclass", actionclass);
			return "queryTeacherScore";
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	@RequestMapping(value = "/exportTeacherScore.do")
	public String exportTeacherScore() {
		try {

			List<Teacher> list = service.selectAll();
			for (Teacher teacher : list) {
				double fen = 0d;
				List<ItemCheck> selectAll = itemCheckService.selectAll(ItemCheck.class, MapUtil.createMap("teacherId", teacher.getId(), "typesel", "学生评教"));
				for (ItemCheck itemCheck : selectAll) {
					fen += itemCheck.getScore();
				}
				if (selectAll.size() > 0) {
					teacher.setXuesheng(MathExtend.round(fen / selectAll.size(), 1));
				}
				teacher.setRen(selectAll.size());

				teacher.setScore(teacher.getXuesheng() * 100 / 100);
				teacher.setScore(MathExtend.round(teacher.getScore(), 1));

			}

			Collections.sort(list, new Comparator<Teacher>() {

				@Override
				public int compare(Teacher o1, Teacher o2) {
					if (o2.getScore() > o1.getScore())
						return 1;
					if (o2.getScore() == o1.getScore())
						return 0;
					return -1;

				}
			});

			putRequestValue("list", list);

			return "print1";
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

}
